package com.source.tiershop.server;

import javax.xml.datatype.XMLGregorianCalendar;

import webservices.framework.affilinet.svc.authentication.AuthenticationContract;
import webservices.framework.affilinet.svc.authentication.AuthenticationContractGetIdentifierExpirationAffilinetWebserviceFaultFaultFaultMessage;
import webservices.framework.affilinet.svc.authentication.AuthenticationContractGetIdentifierExpirationValidationFaultFaultFaultMessage;
import webservices.framework.affilinet.svc.authentication.AuthenticationContractLogonAffilinetWebserviceFaultFaultFaultMessage;
import webservices.framework.affilinet.svc.authentication.AuthenticationContractLogonValidationFaultFaultFaultMessage;
import webservices.framework.affilinet.types.WebServiceTypes;

public class AuthenticationWrapper {
	
	static AuthenticationContract proxy = null;
	static String credentialToken = null;
	
//	private webservices.framework.affilinet.svc.authentication.ObjectFactory svcObjectFactory =
//	            				new webservices.framework.affilinet.svc.authentication.ObjectFactory();
	private static webservices.framework.affilinet.types.ObjectFactory typesObjectFactory =
								new webservices.framework.affilinet.types.ObjectFactory();

	public static String logon() {
	    /*
	    ObjectFactory to create types that are used in the service
	     */
	   
		/*
		 Create the service endpoint interface
	     */
	    webservices.framework.affilinet.svc.authentication.Authentication service =
	            new webservices.framework.affilinet.svc.authentication.Authentication();
	    /*
	    Create a proxy out of the service endpoint interface
	     */
	    proxy = service.getPort(AuthenticationContract.class);
	    
	   /*
	   Create typed request message which will be used in the service call
	    */
	   
	   webservices.framework.affilinet.types.Logon request = typesObjectFactory.createLogon();
	
	   
	   request.setPassword(typesObjectFactory.createLogonPassword("b2Vo5wd7Fa3jFcTTRyRc"));
	   request.setUsername(typesObjectFactory.createLogonUsername("531127"));
	   request.setWebServiceType(WebServiceTypes.PRODUCT);
	   
	   /*
	   call the service method and process the result
	    */
	   
		try {
			credentialToken =  proxy.logon(request); 
		} catch (AuthenticationContractLogonAffilinetWebserviceFaultFaultFaultMessage e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AuthenticationContractLogonValidationFaultFaultFaultMessage e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return credentialToken;
	}

	public static XMLGregorianCalendar getExpiration(String credentialToken) {
		
		XMLGregorianCalendar tokenDate = null;
		try {
			tokenDate = proxy.getIdentifierExpiration(credentialToken);
		} catch (AuthenticationContractGetIdentifierExpirationAffilinetWebserviceFaultFaultFaultMessage e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AuthenticationContractGetIdentifierExpirationValidationFaultFaultFaultMessage e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tokenDate;
	}

	public static String getCredentialToken() {
		
		return credentialToken;
	}

}
